<template>
  <div class="app-container" v-loading="loading">
    <div class="filter-container">
      <el-input
        style="width: 200px"
        type="tel"
        class="filter-item"
        placeholder="手机"
        v-model="queryList.phone" @keyup.enter.native="search"></el-input>
      <el-input
        style="width: 200px"
        type="email"
        class="filter-item"
        placeholder="邮箱"
        v-model="queryList.email" @keyup.enter.native="search"></el-input>
      <el-button type="primary" icon="el-icon-search" class="filter-item" @click="search">搜索</el-button>
      <el-button class="filter-item" @click="reset">重置</el-button>
    </div>
    <div class="action-container">
      <el-button icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱">
      </el-table-column>
      <el-table-column
        label="可用余额">
        <template slot-scope="scope">
          ${{scope.row.balance}}
        </template>
      </el-table-column>
      <el-table-column
        label="冻结余额">
        <template slot-scope="scope">
          ${{scope.row.freezeBalance}}
        </template>
      </el-table-column>
      <el-table-column
        prop="amazonInfo.receivingName"
        label="收货名">
      </el-table-column>
      <el-table-column
        prop="amazonInfo.evaluateName"
        label="评价名">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间">
      </el-table-column>
      <el-table-column
        label="操作"
        width="270">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.delFlag===1"
            size="mini"
            type="danger"
            @click="disableUser(scope.row.id)">禁用
          </el-button>
          <el-button
            v-else-if="scope.row.delFlag===-1"
            size="mini"
            type="success"
            @click="updateBrush(scope.row.id)">启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-elements="totalElements"
      :change-callback="getBrushList"
      ref="page"></pagination>
  </div>
</template>

<script>
  import {getBrush,  updateBrush} from '@/modules/brush/api/brush'
  import pagination from '@/components/pagination/index'

  export default {
    components: {pagination},
    name: 'user',
    data() {
      return {
        loading: false,
        btnLoading: false,
        multipleSelection: [], // 选中数据-多选
        totalElements: 0, // 数据条数
        queryList: {
          email: '',
          phone: ''
        }, // 搜素数据
        tableData: [], // 表格数据
        addDialog: false, // 添加角色模态框
        form: {
          username: '',
          password: '',
          name: '',
          phone: '',
          email: '',
          roles: [] // 下拉框多选值
        }, // 添加用户数据
        formLabelWidth: '120px',
        options: [],
        editDialog: false, // 编辑角色模态框
        currentRow: {
          id: '',
          name: '',
          phone: '',
          email: '',
          roles: []
        } // 当前表格对象
      }
    },
    mounted() {
      this.getBrushList()
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleDelete(row) {
        this.multipleSelection.push(row)
        this.deleteRoles()
      },
      /*
       * 获取用户列表
       * */
      getBrushList(number, pageSize) {
        this.loading = true
        getBrush(number, pageSize, this.queryList).then(response => {
          let data = response.obj
          console.log(data)
          this.totalElements = data.totalElements
          this.tableData = data.content
          setTimeout(() => {
            this.loading = false
          }, 500)
        }).catch(() => {
          this.loading = false
        })
      },
      /*
       * 禁用用户
       * */
      disableUser(id) {
        this.$confirm('是否禁用该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          disableUser(id).then(response => {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            })
            this.$refs.page.refresh()
          }).catch(() => {
            this.loading = false
          })
        })
      },
      /*
       * 启用用户
       * */
      updateBrush(id) {
        this.$confirm('是否启用该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          this.loading = true
          updateBrush(id).then(response => {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            })
            this.$refs.page.refresh()
          }).catch(() => {
            this.loading = false
          })
        })
      },
      /*
       * 添加用户
       * */
      /*
       * 打开添加用户模态框
       * */
      openAddDialog() {
        this.addDialog = true
        this.getRoles()
      },
      /*
       * 关闭添加用户模态框
       * */
      closeAddDialog() {
        this.addDialog = false
        this.form = {
          username: '',
          password: '',
          name: '',
          phone: '',
          email: '',
          roles: [] // 下拉框多选值
        }
      },
      /*
       * 添加用户
       * */
      addUser() {
        this.loading = true
        this.btnLoading = true
        addUser(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeAddDialog()
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
       * 编辑用户
       * */
      /*
       * 打开编辑用户模态框
       * */
      openEditDialog(row) {
        this.currentRow.id = row.id
        this.currentRow.name = row.name
        this.currentRow.phone = row.phone
        this.currentRow.email = row.email
        this.currentRow.roles = []
        if (row.roles) {
          row.roles.forEach(item => {
            this.currentRow.roles.push(item.id)
          })
        }
        this.editDialog = true
        this.getRoles()
      },
      /*
       * 关闭编辑用户模态框
       * */
      closeEditDialog(row) {
        this.editDialog = false
      },
      /*
       * 编辑用户
       * */
      editUser() {
        this.loading = true
        this.btnLoading = true
        editUser(this.currentRow).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeEditDialog()
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
      * 重置密码
      * */
      resetPassword(id) {
        this.$confirm('是否重置密码?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          resetPassword(id).then(response => {
            this.$message({
              message: response.msg,
              type: 'success'
            })
            this.$refs.page.refresh()
          }).catch(() => {
            this.loading = false
          })
        })
      },
      /*
      * 获取角色列表
      * */
      getRoles() {
        if (!this.options.length) {
          selectRole().then(response => {
            this.options = response.obj
          }).catch(() => {
          })
        }
      },
      /*
     * 刷新
     * */
      refresh() {
        this.$refs.page.refresh()
      },
      /*
      * 搜索
      * */
      search() {
        this.$refs.page.search()
      },
      /*
      * 重置
      * */
      reset() {
        this.queryList = {
          name: '',
          email: '',
          phone: ''
        }
        this.$refs.page.search()
      }
    }
  }
</script>

<style scoped>
  body .el-table th.gutter {
    display: table-cell !important;
  }
</style>
